package com.wolfheader.wolfgame.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wolfheader.wolfgame.Message;
import com.wolfheader.wolfgame.bean.admin.PermissionBean;
import com.wolfheader.wolfgame.controller.BaseController;
import com.wolfheader.wolfgame.dao.admin.PermissionDao;
import com.wolfheader.wolfgame.entity.admin.PermissionEntity;
import com.wolfheader.wolfgame.service.admin.AdminService;
import com.wolfheader.wolfgame.service.admin.PermissionService;

@Controller("adminPermissionController")
@RequestMapping("/admin/permission")
public class PermissionController extends BaseController {

	private static final String template_url = "admin/permission";

	
	/** 索引重定向URL */
	private static final String INDEX_REDIRECT_URL = "redirect:/admin/permission";
	@Autowired
	private PermissionDao permissionDao;

	@Resource(name = "permissionServiceImpl")
	private PermissionService permissionService;

	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest request, ModelMap map) {
		map.addAttribute("permissions", permissionDao.findAll());
		map.addAttribute("menu1", "permission");
		map.addAttribute("menu2", "list");
		return PermissionController.template_url + "/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request, ModelMap map) {
		map.addAttribute("menu1", "permission");
		map.addAttribute("menu2", "add");
		return PermissionController.template_url + "/add";
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String add(@PathVariable Long id, HttpServletRequest request, RedirectAttributes attr) {
		permissionService.delete(id);
		addFlashMessage(attr, Message.success("删除成功"));
		return INDEX_REDIRECT_URL;
	}
	
	@RequestMapping(value = "/addPermission", method = RequestMethod.POST)
	public String addPermission(@Validated PermissionBean bean, HttpServletRequest request, RedirectAttributes attr) {
		permissionService.addPermission(bean);
		addFlashMessage(attr, Message.success("添加成功", INDEX_REDIRECT_URL));
		return INDEX_REDIRECT_URL;
	}
	
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String edit(@PathVariable Long id, @Validated PermissionBean bean, RedirectAttributes attr){
		boolean result = permissionService.modif(id, bean);
		if(result){
			addFlashMessage(attr, Message.success("修改成功"));
		}else{
			addFlashMessage(attr, Message.error("修改失败"));
		}
		return INDEX_REDIRECT_URL;
	}
	

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, RedirectAttributes attr, ModelMap map) {
		PermissionEntity entity = permissionDao.findOne(id);
		if (entity == null) {
			addFlashMessage(attr, Message.error("权限不存在"));
			return INDEX_REDIRECT_URL;
		}
		map.addAttribute("menu1", "permission");
		map.addAttribute("menu2", "list");
		map.addAttribute("permission", entity);
		return PermissionController.template_url + "/edit";
	}

}
